package migration

import (
	"center/db/models"
	"common/macaroon"
	"common/uuid"
	"fmt"
	"math/rand"
)

func (receiver *Migration) mockUserBucket() error {
	uid, _ := uuid.New()
	user := models.User{
		ID:           uid,
		Email:        fmt.Sprintf("wx_%d@qq.com", rand.Intn(100)),
		PasswordHash: []byte("test12345"),
		SecretKey:    []byte("1234567890123456"),
	}
	err := receiver.db.Create(&user).Error
	if err != nil {
		fmt.Println("生成用户数据失败：", err)
	}
	fmt.Printf("用户信息：%+v\n", user)

	apikey, err := macaroon.NewAPIKey(uid, user.SecretKey)
	if err != nil {
		fmt.Println("生成用户apikey失败：", err)
	} else {
		fmt.Println("用户apiKey:", apikey.Serialize())
	}

	bid, _ := uuid.New()
	bucket := models.Bucket{
		ID:      bid,
		Bucket:  "default",
		OwnerId: uid,
	}

	err = receiver.db.Create(&bucket).Error
	if err != nil {
		fmt.Println("生成用户bucket失败：", err)
	}
	fmt.Printf("用户bucket：%+v\n", bucket)
	return err
}

func (receiver *Migration) mockUserBucketWithEmail(email string) error {
	uid, _ := uuid.New()
	user := models.User{
		ID:           uid,
		Email:        email,
		PasswordHash: []byte("test12345"),
		SecretKey:    []byte("1234567890123456"),
	}
	err := receiver.db.Create(&user).Error
	if err != nil {
		fmt.Println("生成用户数据失败：", err)
	}
	fmt.Printf("用户信息：%+v\n", user)

	apikey, err := macaroon.NewAPIKey(uid, user.SecretKey)
	if err != nil {
		fmt.Println("生成用户apikey失败：", err)
	} else {
		fmt.Println("用户apiKey:", apikey.Serialize())
	}

	bid, _ := uuid.New()
	bucket := models.Bucket{
		ID:      bid,
		Bucket:  "default",
		OwnerId: uid,
	}

	err = receiver.db.Create(&bucket).Error
	if err != nil {
		fmt.Println("生成用户bucket失败：", err)
	}
	fmt.Printf("用户bucket：%+v\n", bucket)
	return err
}
func (receiver *Migration) mockNodes(n uint8) error {
	for i := uint8(0); i <= n; i++ {
		uid, _ := uuid.New()
		node := models.Node{
			ID:     uid,
			DevId:  "zkgj050520",
			Ip:     "102.168.43.90",
			Port:   18081,
			Nat:    2,
			Region: "sx",
		}
		err := receiver.db.Create(&node).Error
		if err != nil {
			fmt.Println("生成node数据失败：", err)
		} else {
			fmt.Printf("node信息：%+v\n", node)
		}
	}
	return nil
}
